package com.jdrx.sw.sewage.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jdrx.sw.sewage.beans.dto.AlarmRespUserDTO;
import com.jdrx.sw.sewage.beans.entity.AlarmRespUserPO;
import com.jdrx.sw.sewage.beans.entity.DictPO;
import com.jdrx.sw.sewage.beans.vo.AlarmRespUserVO;
import com.jdrx.sw.sewage.dao.AlarmRespUserDAO;
import com.jdrx.sw.sewage.dao.DictDao;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AlarmRespUserServiceImpl extends BaseServiceImpl {

    public static final Logger logger = LoggerFactory.getLogger(AlarmRespUserServiceImpl.class);

    @Autowired
    private AlarmRespUserDAO alarmRespUserDAO;
    @Autowired
    private DictDao dictDao;

    public List<AlarmRespUserVO> selectAlarmRespUserSetList() {
        return alarmRespUserDAO.selectAlarmRespUserSetList();
    }

    public void alarmRespUserSet(List<AlarmRespUserDTO> list) {
        List<AlarmRespUserVO> alarmRespUserVOS = this.selectAlarmRespUserSetList();
        if (null != alarmRespUserVOS && alarmRespUserVOS.size() <= 0) {
            List<AlarmRespUserPO> listAdd = Lists.newArrayList();
            list.forEach(item -> {
                Map<String, Object> params = Maps.newHashMap();
                params.put("classifyKey", "alarmClassify");
                if (1 == item.getType()) {
                    params.put("value", "qualityForecast");
                    DictPO qualityForecast = dictDao.getByParams(params);
                    AlarmRespUserPO po1 = new AlarmRespUserPO();
                    BeanUtils.copyProperties(item, po1);
                    po1.setAlarmType(qualityForecast.getId());

                    params.put("value", "quality");
                    DictPO quality = dictDao.getByParams(params);
                    AlarmRespUserPO po2 = new AlarmRespUserPO();
                    BeanUtils.copyProperties(item, po2);
                    po2.setAlarmType(quality.getId());

                    listAdd.add(po1);
                    listAdd.add(po2);
                }
                if (2 == item.getType()) {
                    params.put("value", "device");
                    DictPO qualityForecast = dictDao.getByParams(params);
                    AlarmRespUserPO po1 = new AlarmRespUserPO();
                    BeanUtils.copyProperties(item, po1);
                    po1.setAlarmType(qualityForecast.getId());

                    params.put("value", "outofcontact");
                    DictPO quality = dictDao.getByParams(params);
                    AlarmRespUserPO po2 = new AlarmRespUserPO();
                    BeanUtils.copyProperties(item, po2);
                    po2.setAlarmType(quality.getId());

                    listAdd.add(po1);
                    listAdd.add(po2);
                }
            });
            alarmRespUserDAO.alarmRespUserSetAdd(listAdd);
        }

        if (null != alarmRespUserVOS && alarmRespUserVOS.size() > 0)
            alarmRespUserDAO.alarmRespUserSet(list);
    }
}
